package jiyun.com.yiyanmeng;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import adapter.EditInfoSchool1Adapter;
import adapter.EditInfoSchool2Adapter;
import adapter.EditInfoSchool3Adapter;
import adapter.EditInfoTimeAdapter;
import base.BaseActivity;
import bean.InfoSchoolBean;
import bean.InfoSchoolBean2;
import bean.InfoSchoolBean3;
import presenter.EditInfoPresenter;
import utils.RecyclerItemTouchListener;
import utils.ToastUtil;
import view.EditInfoView;

public class EditInfoActivity extends BaseActivity<EditInfoView, EditInfoPresenter> implements EditInfoView, View.OnClickListener {

    /**
     * 返回
     */
    private TextView mImageBack;
    /**
     * 我要报考的时间
     */
    private TextView mTvTime;
    /**
     * 我要报考的院校
     */
    private TextView mTvSchool;
    /**
     * 完成
     */
    private Button mBtSubmit;
    private RecyclerView mRecycler1;
    private List<InfoSchoolBean> mSchooldata1;
    private EditInfoSchool1Adapter mSchool1Adapter;
    private RecyclerView mRecycler_timepopup;
    private EditInfoTimeAdapter mTimeAdapter;
    private List<String> mTimedata;
    private String id1;
    private RecyclerView mRecycler2;
    private RecyclerView mRecycler3;
    private List<InfoSchoolBean2> mSchooldata2 = new ArrayList<>();
    private EditInfoSchool2Adapter mSchool2Adapter;
    private TabLayout mTablayout;
    private String id2;
    private StringBuffer mSb;
    private List<InfoSchoolBean3> mSchooldata3;
    private EditInfoSchool3Adapter mSchool3Adapter;
    private int mPos1;
    private int mPos2;
    private String id3;
    private String mTime;
    private int mSelectedTabPosition = 0;
    private View mLayout_popup;
    private ImageView mIv_close;
    private PopupWindow mPopupWindow;

    @Override
    protected void initData() {
        //初始化时间的数据还有学校的第一个数据
        mPresenter.requestEditInfo_School();
        mPresenter.requestEditInfo_Time();
        //初始化选择学校信息的PopupWindow
        initPopup();
    }

    private void initPopup() {
        //初始化选择学校的PopupWindow
        //初始化选择时间的PopupWindow
        // 获得屏幕的高度
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenHeight = metrics.heightPixels;
        mLayout_popup = LayoutInflater.from(this).inflate(R.layout.layout_schoolpopup, null);
        mIv_close = mLayout_popup.findViewById(R.id.iv_close);
        mRecycler2 = mLayout_popup.findViewById(R.id.recyclerView2);
        mRecycler3 = mLayout_popup.findViewById(R.id.recyclerView3);
        mRecycler1 = mLayout_popup.findViewById(R.id.recyclerView1);
        mTablayout = mLayout_popup.findViewById(R.id.tablayout);
        //设置TabLayout的选项卡选中位置在上次的选中位置
        mTablayout.setScrollPosition(mSelectedTabPosition, 0, true);
        mPopupWindow = new PopupWindow(mLayout_popup, RadioGroup.LayoutParams.MATCH_PARENT, (int) (screenHeight * 0.7));
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setOutsideTouchable(true);//点击弹窗外部，弹窗消失
        //设置动画效果
        mPopupWindow.setAnimationStyle(R.style.Popup_BottomStyle);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void initView() {
        mImageBack = (TextView) findViewById(R.id.image_back);
        mImageBack.setOnClickListener(this);
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mTvTime.setOnClickListener(this);
        mTvSchool = (TextView) findViewById(R.id.tv_school);
        mTvSchool.setOnClickListener(this);
        mBtSubmit = (Button) findViewById(R.id.bt_submit);
        mBtSubmit.setOnClickListener(this);
    }

    @Override
    protected EditInfoPresenter createPresenter() {
        return new EditInfoPresenter();
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_edit_info;
    }

    @Override
    public void showToast(String msg) {
        ToastUtil.show(this, msg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.image_back:
                //类似回退键
                finish();
                break;
            case R.id.tv_time:
                //点击弹出选择时间的PopupWindow
                popup_TimePopupWindow();
                break;
            case R.id.tv_school:
                //点击弹出选择学校的PopupWindow
                popup_SchoolPopupWindow();
                break;
            case R.id.bt_submit:
                if (mTvTime.getText().toString().equals("我要报考的时间")) {
                    showToast("请选择您要报考的时间");
                    return;
                }
                if (mTvSchool.getText().toString().equals("我要报考的院校")) {
                    showToast("请选择您要报考的学校");
                    return;
                }
                //请求submit的数据
                mPresenter.requestEditInfo_Submit(id1, id2, id3, mTime);
                break;
        }
    }

    private void popup_SchoolPopupWindow() {
        //设置TabLayout和下面的RecyclerView联动
        mTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                switch (position) {
                    case 0:
                        //点击第一个选项卡时，RecyclerView1显示，RecyclerView2和RecyclerView3隐藏
                        mRecycler1.setVisibility(View.VISIBLE);
                        mRecycler2.setVisibility(View.GONE);
                        mRecycler3.setVisibility(View.GONE);
                        break;
                    case 1:
                        //点击第二个选项卡时，RecyclerView2显示，RecyclerView1和RecyclerView3隐藏
                        mRecycler2.setVisibility(View.VISIBLE);
                        mRecycler1.setVisibility(View.GONE);
                        mRecycler3.setVisibility(View.GONE);
                        break;
                    case 2:
                        //点击第三个选项卡时，RecyclerView3显示，RecyclerView1和RecyclerView2隐藏
                        mRecycler3.setVisibility(View.VISIBLE);
                        mRecycler1.setVisibility(View.GONE);
                        mRecycler2.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                switch (position) {
                    case 0:
                        //点击第一个选项卡时，RecyclerView1显示，RecyclerView2和RecyclerView3隐藏
                        mRecycler1.setVisibility(View.VISIBLE);
                        mRecycler2.setVisibility(View.GONE);
                        mRecycler3.setVisibility(View.GONE);
                        break;
                    case 1:
                        //点击第二个选项卡时，RecyclerView2显示，RecyclerView1和RecyclerView3隐藏
                        mRecycler2.setVisibility(View.VISIBLE);
                        mRecycler1.setVisibility(View.GONE);
                        mRecycler3.setVisibility(View.GONE);
                        break;
                    case 2:
                        //点击第三个选项卡时，RecyclerView3显示，RecyclerView1和RecyclerView2隐藏
                        mRecycler3.setVisibility(View.VISIBLE);
                        mRecycler1.setVisibility(View.GONE);
                        mRecycler2.setVisibility(View.GONE);
                        break;
                }
            }
        });
        //初始化RecyclerView1
        mRecycler1.setLayoutManager(new LinearLayoutManager(this));
        mSchool1Adapter = new EditInfoSchool1Adapter(mSchooldata1, this);
        mRecycler1.setAdapter(mSchool1Adapter);
        mSchool1Adapter.notifyDataSetChanged();
        //弹出popupWindow
        mPopupWindow.showAtLocation(mLayout_popup, Gravity.BOTTOM, 0, 0);
        //设置阴影
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.alpha = 0.3f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(params);
        //弹窗消失后把阴影恢复为正常值
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.alpha = 1.0f;
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(params);
                //记录弹窗关闭时TabLayout的选中位置
                mSelectedTabPosition = mTablayout.getSelectedTabPosition();
            }
        });

        //关闭弹窗的监听
        mIv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
                mSelectedTabPosition = mTablayout.getSelectedTabPosition();
            }
        });
        initPopRecyclerItemClickListener(mPopupWindow);
    }

    private void initPopRecyclerItemClickListener(final PopupWindow popupWindow) {
        mSb = new StringBuffer();
        //设置第一个RecyclerView条目的点击事件
        mRecycler1.addOnItemTouchListener(new RecyclerItemTouchListener(mRecycler1) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {
                mPos1 = vh.getAdapterPosition();
                id1 = mSchooldata1.get(mPos1).getId();
                //请求第二个RecyclerView的数据
                mPresenter.requestEditInfo_School2(id1);
            }
        });

        //设置第二个RecyclerView条目的点击事件
        mRecycler2.addOnItemTouchListener(new RecyclerItemTouchListener(mRecycler2) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {
                mPos2 = vh.getAdapterPosition();
                id2 = mSchooldata2.get(mPos2).get_$7().getId();
                //请求第三个RecyclerView的数据
                mPresenter.requestEditInfo_School3(id1, id2);
            }
        });
        //设置第三个RecyclerView条目的点击事件
        mRecycler3.addOnItemTouchListener(new RecyclerItemTouchListener(mRecycler3) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {
                int pos = vh.getAdapterPosition();
                String name3 = mSchooldata3.get(pos).getZhuan_name();
                id3 = mSchooldata3.get(pos).getId();
                //根据选中内容改变TextView上的文字
                String name1 = mTablayout.getTabAt(0).getText().toString();
                String name2 = mTablayout.getTabAt(1).getText().toString();
                mSb.append(name1 + "-" + name2 + "-" + name3);
                mTvSchool.setText(mSb.toString());
                //关闭popupWindow
                popupWindow.dismiss();
                mSelectedTabPosition = mTablayout.getSelectedTabPosition();
            }
        });
    }

    private void popup_TimePopupWindow() {
        //初始化选择时间的PopupWindow
        // 获得屏幕的高度
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenHeight = metrics.heightPixels;
        View inflate = LayoutInflater.from(this).inflate(R.layout.layout_timepopup, null);
        ImageView iv_close = inflate.findViewById(R.id.iv_close);
        //初始化RecyclerView
        mRecycler_timepopup = inflate.findViewById(R.id.recyclerView_timepopup);
        mRecycler_timepopup.setLayoutManager(new LinearLayoutManager(this));
        mTimeAdapter = new EditInfoTimeAdapter(mTimedata, this);
        mRecycler_timepopup.setAdapter(mTimeAdapter);
        mTimeAdapter.notifyDataSetChanged();
        // mRecycler_timepopup.addOnItemTouchListener(new RecyclerItemTouchListener());
        final PopupWindow popupWindow = new PopupWindow(inflate, RadioGroup.LayoutParams.MATCH_PARENT, (int) (screenHeight * 0.47));
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);//点击弹窗外部，弹窗消失
        //设置阴影
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.alpha = 0.3f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(params);
        //弹窗消失后把阴影恢复为正常值
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.alpha = 1.0f;
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(params);
            }
        });

        //设置动画效果
        popupWindow.setAnimationStyle(R.style.Popup_BottomStyle);

        //弹出动画
        popupWindow.showAtLocation(inflate, Gravity.BOTTOM, 0, 0);

        //为RecyclerView设置接口
        mTimeAdapter.setMyOnClickListener(new EditInfoTimeAdapter.MyOnClickListener() {
            @Override
            public void myonclick(int position) {
                //点击条目选择时间后，将所选的内容显示在TextView上
                mTime = mTimedata.get(position);
                mTvTime.setText(mTime);
                //然后关闭弹窗
                popupWindow.dismiss();
                mSelectedTabPosition = mTablayout.getSelectedTabPosition();
            }
        });

        //关闭弹窗
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                mSelectedTabPosition = mTablayout.getSelectedTabPosition();
            }
        });
    }

    @Override
    public void onSchoolSuccess(List<InfoSchoolBean> data) {
        if (data != null) {
            mSchooldata1 = data;
        }
    }

    @Override
    public void onSchoolSuccess2(InfoSchoolBean2 infoSchoolBean2s) {
        if (infoSchoolBean2s != null) {
            if (mSchooldata2.size() > 0) {
                mSchooldata2.clear();
            }
            mSchooldata2.add(infoSchoolBean2s);
            //初始化RecyclerView2
            mRecycler2.setLayoutManager(new LinearLayoutManager(EditInfoActivity.this));
            mSchool2Adapter = new EditInfoSchool2Adapter(mSchooldata2, EditInfoActivity.this);
            mRecycler2.setAdapter(mSchool2Adapter);
            mSchool2Adapter.notifyDataSetChanged();
            if (mRecycler1.getVisibility() == View.VISIBLE) {
                mRecycler1.setVisibility(View.GONE);
            }
            mRecycler2.setVisibility(View.VISIBLE);
            //根据选中内容，改变第一个Tabitem的标题
            mTablayout.getTabAt(0).setText(mSchooldata1.get(mPos1).getName());
            //设置第二个条目选中
            mTablayout.setScrollPosition(1, 0, true);
        }
    }

    @Override
    public void onSchoolSuccess3(List<InfoSchoolBean3> infoSchoolBean3s) {
        if (infoSchoolBean3s != null) {
            mSchooldata3 = infoSchoolBean3s;
            //初始化RecyclerView3
            mRecycler3.setLayoutManager(new LinearLayoutManager(EditInfoActivity.this));
            mSchool3Adapter = new EditInfoSchool3Adapter(mSchooldata3, EditInfoActivity.this);
            mRecycler3.setAdapter(mSchool3Adapter);
            mSchool3Adapter.notifyDataSetChanged();
            if (mRecycler2.getVisibility() == View.VISIBLE) {
                mRecycler2.setVisibility(View.GONE);
            }
            mRecycler3.setVisibility(View.VISIBLE);
            //设置第三个条目选中
            mTablayout.setScrollPosition(2, 0, true);
            //根据选中内容，改变第二个Tabitem的标题
            mTablayout.getTabAt(1).setText(mSchooldata2.get(mPos2).get_$7().getZhuan_name());
        }
    }

    @Override
    public void onTimeSuccess(List<String> timeBean) {
        if (timeBean != null) {
            mTimedata = timeBean;
        }
    }

    @Override
    public void onSubmit(Void voiddata) {
        //提交信息的接口请求成功
        //关闭本页面
        finish();
        showToast("成功！");
        //跳转到登录界面
        startActivity(new Intent(EditInfoActivity.this, LoginActivity.class));
    }

    @Override
    public void onFail(String error) {
    }
}